package com.lvtulife.paas.gtd.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lvtulife.common.bean.DelVo;
import com.lvtulife.common.component.constants.Constant;
import com.lvtulife.common.component.exception.ErrMsg;
import com.lvtulife.common.service.impl.BaseServiceImpl;
import com.lvtulife.paas.gtd.bean.StrategyVo;
import com.lvtulife.paas.gtd.entity.GtdStrategy;
import com.lvtulife.paas.gtd.mapper.GtdStrategyMapper;
import com.lvtulife.paas.gtd.service.GtdStrategyServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * GtdStrategy 表数据服务层接口实现类
 */
@Service
@Transactional(readOnly = true)
public class GtdStrategyServiceImpl extends BaseServiceImpl<GtdStrategyMapper, GtdStrategy> implements GtdStrategyServiceI {

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Long saveStrategy(StrategyVo vo) {
        GtdStrategy entity = initBean(vo);

        int count = vo.getId() != null ? baseMapper.updateById(entity) : baseMapper.insert(entity);
        checkOperation(count);

        return entity.getId();
    }


    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void sortStrategy(Long id, Integer sort) {
        GtdStrategy entity = getEntity(id);
        entity.setSort(sort == null || sort <= 0 ? 1 : sort);
        updateEntity(entity);
    }


    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void openStrategy(Long id, Boolean isOpen) {
        GtdStrategy entity = getEntity(id);
        entity.setIsOpen(isOpen ? Constant.Base.Open.OPEN : Constant.Base.Open.CLOSE);
        updateEntity(entity);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void delStrategy(DelVo delVo) {
        verifyParams(delVo);
        delStrategy(delVo.getId(), delVo.getUserId());
    }


    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public void multiDelStrategy(DelVo delVo) {
        verifyParams(delVo);

        for (Long id : delVo.getIds()) {
            delStrategy(id, delVo.getUserId());
        }
    }

    @Override
    public List<GtdStrategy> findStageStrategy(Long userId, Integer stage) {
        EntityWrapper<GtdStrategy> wrapper = new EntityWrapper<>();
        wrapper.where("uid={0} and stage={1}", userId, stage);
        return selectList(wrapper);
    }

    @Override
    public List<GtdStrategy> findThemeStrategy(Long userId, Integer themeId) {
        verifyParams(userId, themeId);

        EntityWrapper<GtdStrategy> wrapper = new EntityWrapper<>();
        wrapper.where("uid={0} and tid={1}", userId, themeId);
        return selectList(wrapper);
    }


    private void delStrategy(Long id, Long userId) {
        verifyParams(id, userId);

        GtdStrategy entity = getEntity(id);

        if (!entity.getUid().equals(userId)) {
            throw new RuntimeException(ErrMsg.PERMISSION_DENIED.getMsg());
        }

        entity.setDel(Constant.Base.State.DEL);
        updateEntity(entity);
    }

    private void updateEntity(GtdStrategy entity) {
        entity.setUpdatedDt(new Date());
        checkOperation(updateById(entity));
    }

    private GtdStrategy initBean(StrategyVo vo) {
        verifyParams(vo);

        GtdStrategy entity = null;
        if (vo.getId() != null) {
            entity = getEntity(vo.getId());

            /* 可修改项 */
            if (StringUtils.isBlank(vo.getContent()))
                entity.setContent(vo.getContent());
        } else {
            verifyParams(vo.getThemeId(), vo.getUserId(), vo.getStage(), vo.getContent());

            entity = new GtdStrategy();
            entity.setSort(1);
            entity.setIsOpen(Constant.Gtd.Open.CLOSE);
            entity.setCreatedDt(new Date());
            entity.setUpdatedDt(entity.getCreatedDt());
            entity.setDel(Constant.Base.State.NORMAL);
            /* 必填项 */
            entity.setContent(vo.getContent());
            entity.setStage(vo.getStage());
            entity.setTid(vo.getThemeId());
            entity.setUid(vo.getUserId());
        }
        return entity;
    }
}